<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="/views/common/taglibs.jsp" %>
<link rel="stylesheet" type="text/css" href="${ctx}/css/common/search/search.css"/>
<link rel="stylesheet" href="${ctx}/css/widget/bootstrap/bootstrap-table.min.css"/>
<script type="text/javascript" src="${ctx}/js/widget/bootstrap/bootstrap-table.min.js"></script>
<script type="text/javascript" src="${ctx}/js/widget/bootstrap/bootstrap-table-zh-CN.min.js"></script>
<!-- 按钮触发模态框 -->
<button id="myModalBtn" class="btn btn-primary btn-lg" style="display: none;" data-toggle="modal" data-target="#myModal">触发模态框</button>
<!-- 模态框（Modal） -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title modalTitle" id="myModalLabel">查询</h4>
            </div>
            <div class="modal-body">
                <!-- 展示、隐藏搜索条件线条部分div -->
                <%--<div class="list-bar"></div>--%>
                <!-- 加载bootstrap表格组件div      -->
                <div class="employee_div">
                    <table id="searchTable" class="table"></table>
                    <%--<table id="searchTable"
                           data-striped="true"
                           data-toggle="table"
                           data-cookie="false"
                           data-pagination="true"
                           data-search="false"
                           data-show-refresh="false"
                           data-side-pagination="server"
                           class="table">
                        <thead id="bootstrapBody">
                        <tr>
                            &lt;%&ndash;bootstrap显示字段&ndash;%&gt;
                        </tr>
                        </thead>
                    </table>--%>
                </div>

                <!-- 引入通用搜索条件js文件xiaogq -->
                <script type="text/javascript" src="${ctx}/js/common/search/search.js"></script>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="commit">提交</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

<script>
    //全局搜索变量
    var searchVar = "";
    //输入框标签
    var inputVar;
    //隐藏标签
    var hiddenVar;
    //搜索id
    var searchId;
    //表名
    var tableCode;
    //是否是加入相关关系(xioagq)
    var addReFlag;
    //主实体id(xioagq)
    var hostRecordId;

    /**
     * 点击搜索弹窗事件
     * @param    node              当前节点
     * @param    flag              单选多选标识(true:多选,false:单选)
     * @param    addRelationFlag   是否是加入相关关系标识参数(true:是,false:否)
     * @param    hostId            主实体id
     * @param    reId              相关实体主记录id
     */
    function filterQuoteClick(node,flag,addRelationFlag,hostId,reId) {
        $(".employee_div tr").empty();
        searchVar = "";
        inputVar = "";
        var spanId = node.id;
        addReFlag = (addRelationFlag == undefined || addRelationFlag == null || addRelationFlag == "") ? false :  addRelationFlag;
        hostRecordId = (hostId == undefined || hostId == null) ? "" :  hostId;
        var relatedId = (reId == undefined || reId == null) ? "" :  reId;
        inputVar = $(node);
        hiddenVar = $(node).prev();
        var tableStr = spanId.substring(spanId.indexOf('k')+4, spanId.indexOf('s')-1);
        var tableNumber = tableStr.substring(0,tableStr.indexOf("_"));
        tableCode = tableNumber;
        $("#searchTableBtn").attr("tableCode",tableNumber);
        //获取搜索内容
        $.ajax({
            url:"${ctx}/CommonOperation/searchCondition.do?isTable=true",
            type:"get",
            dataType:"json",
            data:{"tableCode":tableNumber},
            success:function(data){
                console.log(data);
                var tableUrl = '${ctx}/CommonOperation/querySearch.do?tableCode='+tableNumber+"&isTable="+data.isTable+"&fields="+data.fields+"&searchIds="+relatedId;
                $("#searchTable").attr("data-url",tableUrl);
                var columnArr = [];
                if(flag){//复选框
                    columnArr.push({
                        checkbox:true,
                        field:'ck'
                    });
                }else{//单选框
                    columnArr.push({
                        radio:true,
                        field:'ck'
                    });
                }
                for(var i=0;i<data.fieldsList.length;i++){
                    columnArr.push({title:data.fieldsList[i].inchinese,field:data.fieldsList[i].name});
                };
/*                $("#searchTable").html("");*/
                $("#searchTable").bootstrapTable('destroy').bootstrapTable({
                    data: data.resultMap.rows,
                    pageList:[10,15,20],
                    showColumns: true,  //显示下拉框勾选要显示的列
                    search:true,
                    showRefresh:true,
                    columns: columnArr
                });
                $("#searchTable").bootstrapTable('hideColumn', 'id');
                $("#searchTable").bootstrapTable('refresh', {url: tableUrl});
            },
            error:function (textStatus, errorThrown) {
                console.log(textStatus);
            }
        });
        $("#myModalBtn").click();

    }

    /**
     * 选中单选框，单击行选中，双击行提交功能
     */
    $('#searchTable').on('check.bs.table', function (e, row, $element) {
        searchVar = row.name;
        searchId = row.id;
    });
    $('#searchTable').on('click-row.bs.table', function (e, row, $element) {
        $element.children(".bs-checkbox").children("input").trigger("click");
        searchVar = row.name;
        searchId = row.id;
    });
    $('#searchTable').on('dbl-click-row.bs.table', function (e, row, $element) {
        $("#commit").click();
    });
    //提交操作
    $("#commit").click(function () {
        if(addReFlag){
            //得到选择的相关实体记录id
            var items = $('#searchTable').bootstrapTable('getSelections');
            if(items.length > 0){
                var rIdlist = [];
                for(i = 0; i < items.length; i++){
                    rIdlist.push( items[i].id );
                }
                addEntityRelation(hostRecordId,rIdlist);
            }
        }else{
            inputVar.val(searchVar);
            hiddenVar.val(searchId);
        }
        $("#myModalBtn").click();
    });

    //保存实体与实体之间的相关关系(xiaogq)
    function addEntityRelation(hostId,relatedIds){

    }
</script>
